import * as YvanUI from 'yvan-ui-wotu'
import {Admin} from "./admin/index"
import * as debugTool from './debugTool'

const React = YvanUI.React
const {render} = YvanUI.ReactDOM
const _ = YvanUI.lodash
const moment = YvanUI.moment

window['wechatAPI'] = 'http://healthbot.dev.jzterp.net'
window['hideExample'] = false

const ajaxInner = YvanUI.createAjax({
    // baseUrl: 'http://healthbot.dev.jzterp.net',            // 小九微信
    // baseUrl: 'http://kgservice-backend.prod.jzterp.net',    // 知识图谱测试环境
    // baseUrl: 'http://10.2.123.150:9090/',    // 测试环境
    baseUrl: '/',
    timeout: 6000
})

const ajax = function (option): Promise<any> {
    return new Promise<any>((resolve, reject) => {
        ajaxInner(option).then(res => {
            resolve(res.rawData);

        }).catch(e => {
            YvanUI.notifyError({
                message: '网络异常',
                description: <pre>{JSON.stringify(e, null, ' ')}</pre>,
                duration: 60
            })
            reject(e)
        })
    })
};

YvanUI.extend({
    ajax,
    debugMode: true,
    serverJsUrlTransform(url: string) {
        return '/!' + url
    },
    dict: {is_enable: [{id: '-1', text: "停用"}, {id: '1', text: "启用"}]},
    formatter: {
        fmtDate(javaTimestamp: string) {
            // const moment = _.get(window, 'moment');
            if (!javaTimestamp) {
                return '';
            }
            if (typeof javaTimestamp === 'string') {
                return moment(_.toInteger(javaTimestamp)).format('YYYY-MM-DD HH:mm:ss')
            }
            var timestamp = moment(javaTimestamp);
            return timestamp.format('YYYY-MM-DD HH:mm:ss');
        },
        fmtDateNoTime(javaTimestamp: string) {
            // const moment = _.get(window, 'moment');
            if (!javaTimestamp) {
                return '';
            }
            if (typeof javaTimestamp === 'string') {
                return moment(_.toInteger(javaTimestamp)).format('YYYY-MM-DD')
            }
            var timestamp = moment(javaTimestamp);
            return timestamp.format('YYYY-MM-DD');
        }
    },
})

window['fmtDate'] = (javaTimestamp: string) => {
    if (!javaTimestamp) {
        return '';
    }
    if (typeof javaTimestamp === 'string') {
        return moment(parseInt(javaTimestamp)).format('YYYY-MM-DD HH:mm:ss')
    }
    var timestamp = moment(javaTimestamp);
    return timestamp.format('YYYY-MM-DD HH:mm:ss');
}

window['fmtDateNoTime'] = (javaTimestamp: string) => {
    if (!javaTimestamp) {
        return '';
    }
    if (typeof javaTimestamp === 'string') {
        return moment(parseInt(javaTimestamp)).format('YYYY-MM-DD')
    }
    var timestamp = moment(javaTimestamp);
    return timestamp.format('YYYY-MM-DD');
}

export default function (mountTo: HTMLElement) {
    window['debugTool'] = debugTool

    render(
        <Admin/>,
        mountTo
    )
}
